package com.android.fragmentdemo.defineviews;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;

public class MonitorView extends View {

    private static final String LOG_TAG = "MonitorView";

    public MonitorView(Context context, @Nullable AttributeSet attributeSet) {
        super(context, attributeSet);
        setOnGenericMotionListener((view, event) -> {
            InputDevice inputDevice = event.getDevice();
            if (inputDevice != null && MotionEvent.ACTION_DOWN == event.getAction()) {
                InputDevice.MotionRange motionRange = inputDevice.getMotionRange(MotionEvent.AXIS_LTRIGGER);
                float rangeValue = motionRange.getRange();
                Log.d(LOG_TAG, "rangeValue:" + rangeValue);
                //获取手柄扳机值
                float leftAxisValue = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
                float rightAxisValue = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
                Log.d(LOG_TAG, "leftAxisValue:" + leftAxisValue + ",rightAxisValue:" + rightAxisValue);
            }
            return false;
        });
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.d(LOG_TAG, "keyCode:" + event.getKeyCode());
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK: {
                Log.d(LOG_TAG, "KeyEvent.KEYCODE_BACK");
                break;
            }
            case KeyEvent.KEYCODE_MENU: {
                Log.d(LOG_TAG, "KeyEvent.KEYCODE_MENU");
                break;
            }
            case KeyEvent.KEYCODE_VOLUME_DOWN: {
                Log.d(LOG_TAG, "音量减小");
                break;
            }
            case KeyEvent.KEYCODE_VOLUME_UP: {
                Log.d(LOG_TAG, "音量增大");
                break;
            }
            default: {
                break;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
